<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Fanzhanao
 * Date: 11-4-11
 * Time: 下午2:20
 * To change this template use File | Settings | File Templates.
 */
 
class renren_func {
    /**
     * @var string 人人api地址
     */
    var $RR_API_SERVER = 'http://api.renren.com/restserver.do?';

    /**
     * @var string APP密钥
     */
    var $RR_CONNECT_API_KEY  =   'f8fd0175eca34eaabb7f6e377ad4283e';

    var $RR_CONNECT_SECRET  =   '586c830b163149db890af7910ae542e6';

    public function __construct(){
        
    }

    function rr_get_session() {
	    $connect_session = array (
									"user" => $_COOKIE[RR_CONNECT_API_KEY.'_user'],
									"session_key" => $_COOKIE[RR_CONNECT_API_KEY.'_session_key'],
									"ss" => $_COOKIE[RR_CONNECT_API_KEY.'_ss'],
									"expires" => $_COOKIE[RR_CONNECT_API_KEY.'_expires'],
									);

	    return $connect_session;
    }
    
    public function rr_generate_sig($params, $secret=NULL) {
        ksort($params);
	    $sig = '';
	    foreach($params as $key=>$value) {
		    $sig .= "$key=$value";
	    }
        $secret && $secret = $this->RR_CONNECT_SECRET;
	    $sig .= $secret;

	    return md5($sig);
    }


    function rr_verify() {
        $session = $this->rr_get_session();

	    if(empty($session) || empty($session['expires']) || time() > intval($session['expires']) )
		    return false;
	    else
            return rr_generate_sig($session) == $_COOKIE[RR_CONNECT_API_KEY];
    }

    
    function rr_get_id() {
	    return $_COOKIE[RR_CONNECT_API_KEY.'_user'];
    }

    function rr_get_session_key() {
	    return $_COOKIE[RR_CONNECT_API_KEY.'_session_key'];
    }


    
}
